package com.example.compreshensiveshop.service.imp;

import cn.hutool.http.HttpRequest;
import com.example.compreshensiveshop.dao.SellerDao;
import com.example.compreshensiveshop.dao.UserRoleDao;
import com.example.compreshensiveshop.dto.SellerCreateDTO;
import com.example.compreshensiveshop.dto.SellerUpdateDTO;
import com.example.compreshensiveshop.po.SellerPO;
import com.example.compreshensiveshop.po.UserRolePO;
import com.example.compreshensiveshop.service.SellerService;
import com.example.compreshensiveshop.vo.SellerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Seller Service Implementation
 */
@Service
public class SellerServiceImpl implements SellerService {
    @Autowired
    private SellerDao sellerDao;
    @Autowired
    private UserRoleDao userRoleDao;
    @Override
    public void addSeller(SellerCreateDTO sellerCreateDTO) {
        SellerPO sellerPO = SellerPO.builder()
                .userId(sellerCreateDTO.getUserId())
                .shopName(sellerCreateDTO.getShopName())
                .contactPerson(sellerCreateDTO.getContactPerson())
                .contactPhone(sellerCreateDTO.getContactPhone())
                .address(sellerCreateDTO.getAddress())
                .logoUrl(sellerCreateDTO.getLogoUrl())
                .build();
        sellerDao.addSeller(sellerPO);
        userRoleDao.addSellerRole(UserRolePO.builder()
                .userId(sellerCreateDTO.getUserId())
                .roleId(2L)
                .build());
    }

    @Override
    public SellerPO queryById(Long sellerId) {
        return sellerDao.queryById(sellerId);
    }

    @Override
    public SellerVO querySellerById(Long id){
        SellerPO sellerPO = sellerDao.queryById(id);
        SellerVO sellerVO = SellerVO.builder()
                .id(sellerPO.getId())
                .userId(sellerPO.getUserId())
                .shopName(sellerPO.getShopName())
                .contactPerson(sellerPO.getContactPerson())
                .contactPhone(sellerPO.getContactPhone())
                .address(sellerPO.getAddress())
                .logoUrl(sellerPO.getLogoUrl()).build();
        return sellerVO;
    }
    @Override
    public Long querySellerId(Long id){
        return sellerDao.querySellerId(id);
    }

    @Override
    public void updateLogo(Long sellerId, String logoUrl) {
        sellerDao.updateLogo(sellerId, logoUrl);
    }

    @Override
    public void updateMessage(SellerUpdateDTO sellerUpdateDTO) {
        SellerPO sellerPO = SellerPO.builder()
                .id(sellerUpdateDTO.getId())
                .shopName(sellerUpdateDTO.getShopName())
                .contactPerson(sellerUpdateDTO.getContactPerson())
                .contactPhone(sellerUpdateDTO.getContactPhone())
                .address(sellerUpdateDTO.getAddress())
                .logoUrl(sellerUpdateDTO.getLogoUrl())
                .build();
        sellerDao.updateMessage(sellerPO);
    }
}